package com.example.app23_graphics;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class DrawActivity extends AppCompatActivity {

    private static final String TAG = "DrawActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 自定义视图
        setContentView(new MyView(this));
        Log.i(TAG, "onCreate()");
    }

    /**
     * 自定义视图
     */
    static class MyView extends View {

        private final ShapeDrawable shapeDrawable;
        private final Paint paint;

        public MyView(Context context) {
            super(context);
            shapeDrawable = new ShapeDrawable(new OvalShape());
            // 指定颜色
            shapeDrawable.getPaint().setColor(Color.RED);
            // 指定位置
            shapeDrawable.setBounds(10, 10, 400, 200);

            paint = new Paint();
            // 字体颜色
            paint.setColor(Color.BLUE);
            // 字体大小
            paint.setTextSize(36);
            // 粗体
            paint.setTypeface(Typeface.DEFAULT_BOLD);
            // 消除锯齿
            paint.setAntiAlias(true);
        }

        @Override
        protected void onDraw(@NonNull Canvas canvas) {
            super.onDraw(canvas);
            // 绿色背景
            canvas.drawColor(Color.GREEN);
            // 椭圆
            shapeDrawable.draw(canvas);
            // 文本
            canvas.drawText("我是一段文字，哈哈哈", 10, 260, paint);
        }
    }
}